package com.eseo.munchkinarbiter;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

public class FightEventListener extends GameChangeListener implements OnClickListener {
	static Random RAND = new Random();
	
	private GameData gameData;
	private Context context;
	
	private Button monsterLvlPlusButton;
	private Button monsterLvlMinusButton;
	private TextView monsterLvlLabel;
	private Button monsterModPlusButton;
	private Button monsterModMinusButton;
	private TextView monsterModLabel;
	private Button playerModPlusButton;
	private Button playerModMinusButton;
	private TextView playerModLabel;
	private TextView resultLabel;
	private TextView strenghtLabel;
	private Button flightButton;
	private Button helpButton;
	
	public FightEventListener(View rootView, GameData gameData) {
		this.gameData = gameData;
		this.context = rootView.getContext();
		
		monsterLvlPlusButton = (Button) rootView.findViewById(R.id.monsterlvlmore);
		monsterLvlMinusButton = (Button) rootView.findViewById(R.id.monsterlvlminus);
		monsterLvlLabel = (TextView) rootView.findViewById(R.id.lvlmonsterlabel);
		monsterModPlusButton = (Button) rootView.findViewById(R.id.monstermodmore);
		monsterModMinusButton = (Button) rootView.findViewById(R.id.monstermodminus);
		monsterModLabel = (TextView) rootView.findViewById(R.id.modmonsterlabel);
		playerModPlusButton = (Button) rootView.findViewById(R.id.playermodmore);
		playerModMinusButton = (Button) rootView.findViewById(R.id.playermodminus);
		playerModLabel = (TextView) rootView.findViewById(R.id.modplayerlabel);
		resultLabel = (TextView) rootView.findViewById(R.id.resultlabel);
		strenghtLabel = (TextView) rootView.findViewById(R.id.playerstrengthlabel);
		flightButton = (Button) rootView.findViewById(R.id.playerroll);
		helpButton = (Button) rootView.findViewById(R.id.playerhelp);

		monsterLvlPlusButton.setOnClickListener(this);
		monsterLvlMinusButton.setOnClickListener(this);
		monsterModPlusButton.setOnClickListener(this);
		monsterModMinusButton.setOnClickListener(this);
		playerModPlusButton.setOnClickListener(this);
		playerModMinusButton.setOnClickListener(this);
		flightButton.setOnClickListener(this);
		helpButton.setOnClickListener(this);
		
	}
	
	public void onPlayerFightStrengthChanged(GameData gameData) {
		super.onPlayerFightStrengthChanged(gameData);
		strenghtLabel.setText(Integer.toString(gameData.getFight().getPlayerStrength()));
	}
	
	public void onMonsterLevelChanged(GameData gameData) {
		super.onMonsterLevelChanged(gameData);
		monsterLvlLabel.setText(Integer.toString(gameData.getFight().getMonsterLevel()));
	}
	
	public void onMonsterModChanged(GameData gameData) {
		super.onMonsterModChanged(gameData);
		monsterModLabel.setText(Integer.toString(gameData.getFight().getMonsterModifier()));
	}

	public void onPlayerModChanged(GameData gameData) {
		super.onPlayerModChanged(gameData);
		playerModLabel.setText(Integer.toString(gameData.getFight().getPlayerModifier()));
	}
	
	public void onFightResultChanged(GameData gameData) {
		super.onFightResultChanged(gameData);
		resultLabel.setText(gameData.getFight().getResult() ? context.getString(R.string.result_won) : context.getString(R.string.result_lost));
	}
	
	public void onClick(View v) {
		if(v == monsterLvlPlusButton) {
			gameData.getFight().incMonsterLevel();
		} else if(v == monsterLvlMinusButton) {
			gameData.getFight().decMonsterLevel();
		} else if(v == monsterModPlusButton) {
			gameData.getFight().incMonsterMod();
		} else if(v == monsterModMinusButton) {
			gameData.getFight().decMonsterMod();
		} else if(v == playerModPlusButton) {
			gameData.getFight().incPlayerMod();
		} else if(v == playerModMinusButton) {
			gameData.getFight().decPlayerMod();
		} else if(v == flightButton) {
			int value = Math.abs(RAND.nextInt()) % 6 + 1;
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(R.string.diceroll_title);
			alert.setMessage(context.getString(R.string.diceroll_result, value));
			alert.setPositiveButton(R.string.ok, null);
			alert.show();
		} else if(v == helpButton) {
			final ArrayList<PlayerStats> players = new ArrayList<PlayerStats>();
			String[] playersName = new String[gameData.getPlayers().size()];

			playersName[0] = "Aucun";
			int i = 1;
			for(PlayerStats p : gameData.getPlayers()) {
				if(p != gameData.getCurrentPlayer()) {
					players.add(p);
					playersName[i] = p.getName();
					i++;
				}
			}
			
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(R.string.fighthelp_title);
			final NumberPicker picker = new NumberPicker(context);
			picker.setMinValue(0);
			picker.setMaxValue(playersName.length-1);
			picker.setDisplayedValues(playersName);
			alert.setView(picker);
			alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(picker.getValue() == 0)
						gameData.getFight().setHelpPlayer(null);
					else
						gameData.getFight().setHelpPlayer(players.get(picker.getValue() - 1));
				}
			});
			alert.show();
		}
	}
}
